package com.shop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.commom.enums.YesORNo;
import com.shop.commom.utils.DesensitizationUtil;
import com.shop.commom.utils.JSONResult;
import com.shop.commom.utils.PagedGridResult;
import com.shop.pojo.Items;
import com.shop.pojo.ItemsImg;
import com.shop.pojo.ItemsParam;
import com.shop.pojo.ItemsSpec;
import com.shop.pojo.vo.CommentLevelCountVo;
import com.shop.pojo.vo.ItemCommentVo;
import com.shop.pojo.vo.SearchItemVo;
import com.shop.service.CategoryService;
import com.shop.service.ItemsService;
import com.shop.vo.ItemInfoVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2020-10-31
 */
@RestController
@RequestMapping("items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @GetMapping("/info/{id}")
    public JSONResult index(@PathVariable String id){
        Items items = itemsService.queryItemById(id);
        List<ItemsImg> itemsImgs = itemsService.queryItemImgList(id);
        List<ItemsSpec> itemsSpecs = itemsService.queryItemSpecList(id);
        ItemsParam itemsParam = itemsService.queryItemParam(id);

        ItemInfoVo itemInfoVo = new ItemInfoVo();
        itemInfoVo.setItems(items);
        itemInfoVo.setItemsImgList(itemsImgs);
        itemInfoVo.setItemSpecList(itemsSpecs);
        itemInfoVo.setItemParams(itemsParam);
        return JSONResult.OK(itemInfoVo);
    }


    @GetMapping("/commentLevel")
    public JSONResult commentLevel(@RequestParam String itemId){
        CommentLevelCountVo commentLevelCountVo = itemsService.queryCommentCounts(itemId);
        return JSONResult.OK(commentLevelCountVo);
    }

    @GetMapping("/comments")
    public JSONResult comments(@RequestParam("itemId") String itemId,
                               @RequestParam("level")Integer level,
                               @RequestParam("page") Integer page,
                               @RequestParam("pageSize")Integer pageSize){
        Page pages = new Page(page,pageSize);
        IPage<ItemCommentVo> objects = itemsService.queryPageComment(itemId, level, pages);
        PagedGridResult result = new PagedGridResult();
        result.setPage(page);
        result.setRecords(result.getTotal());
        List<ItemCommentVo> records = objects.getRecords();
        records.forEach(itemCommentVo -> {
            itemCommentVo.setNickname(DesensitizationUtil.commonDisplay(itemCommentVo.getNickname()));
        });
        result.setRows(records);
        result.setTotal((int) objects.getTotal());
        return JSONResult.OK(result);
    }

    @GetMapping("/search")
    public JSONResult search(@RequestParam("keywords") String keywords,
                               @RequestParam("sort") String sort,
                               @RequestParam("page") Integer page,
                               @RequestParam("pageSize")Integer pageSize){
        Page pages = new Page(page,pageSize);
        IPage<SearchItemVo> objects = itemsService.searchItems(keywords, sort, pages);
        PagedGridResult result = new PagedGridResult();
        result.setPage(page);
        result.setRecords(objects.getTotal());
        result.setRows(objects.getRecords());
        result.setTotal((int) objects.getTotal());
        return JSONResult.OK(result);
    }

    @GetMapping("/catItems")
    public JSONResult catItems(@RequestParam("catId") String catId,
                               @RequestParam("sort") String sort,
                               @RequestParam("page") Integer page,
                               @RequestParam("pageSize")Integer pageSize){
        Page pages = new Page(page,pageSize);
        IPage<SearchItemVo> objects = itemsService.searchItemByThirdCat(catId, sort, pages);
        PagedGridResult result = new PagedGridResult();
        result.setPage(page);
        result.setRecords(objects.getTotal());
        result.setRows(objects.getRecords());
        result.setTotal((int) objects.getTotal());
        return JSONResult.OK(result);
    }


    @GetMapping("/refresh")
    public JSONResult refresh(@RequestParam String itemSpecIds){
        return JSONResult.OK(itemsService.queryItemsBySpecIds(itemSpecIds));
    }

}

